import analytics from './analytics'

// 1/12 -- 1/14  变成 1/19 -- 1/21
let filter = {
    a8: 0,
    z0: {
            $gte: +new Date('2017/01/12'),
            $lte: +new Date('2017/01/14 23:59:59')
    }
}
const foo = async function() {
    console.log('开始数据转换...')
    const data = await find(filter)
    const arry = []
    let total = 0
    console.log(data.length)
    //const _item
    let _item
    for (const item of data) {
        _item = item.toObject() //转换成简单对象 plain Object
        delete _item['_id']
        delete _item['_snid']
        delete _item['__v']
        //console.log(_item)
        if (_item.z0 >= +new Date('2017/1/12') && _item.z0 <= +new Date('2017/1/12 23:59:59')) {
            _item.z = new Date(item.z.toISOString().replace('2017-01-12', '2017-01-19').replace('2017-01-11', '2017-01-19'))
            _item.a4 = new Date(item.a4.toISOString().replace('2017-01-12', '2017-02-03').replace('2017-01-11', '2017-01-19'))
        } else if (_item.z0 >= +new Date('2017/1/13') && _item.z0 <= +new Date('2017/1/13 23:59:59')) {
            _item.z = new Date(item.z.toISOString().replace('2017-01-13', '2017-01-20').replace('2017-01-12', '2017-01-20'))
            _item.a4 = new Date(item.a4.toISOString().replace('2017-01-13', '2017-02-20').replace('2017-01-12', '2017-01-20'))
        } else if (_item.z0 >= +new Date('2017/1/14') && _item.z0 <= +new Date('2017/1/14 23:59:59')) {
            _item.z = new Date(item.z.toISOString().replace('2017-01-14', '2017-01-21').replace('2017-01-13', '2017-01-21'))
            _item.a4 = new Date(item.a4.toISOString().replace('2017-01-14', '2017-02-21').replace('2017-01-13', '2017-01-21'))
        }
        _item.z0 = +item.z 
        arry.push(_item)
        //console.log(_item)
        if (arry.length >= 100) {
            console.log(`正在转换第${ total }条`)
            await analytics.create(arry)
            arry.length = 0
            total += 100
        }
    }
    if (arry.length > 0) {
            console.log(`正在转换第${ total }条`)
            await analyticscreate(arry)
            total += arry.length
    }
    console.log(`转换结束，共转换了${ total }条`)
}

const random = parseInt(Math.random() * (4222391 - 4158859)) + 4158859  //最小id：4158859 到最大id的随机数，执行2万次，删除两万条数据为止

const deleteData = async function (){
    let random = 0
    const arryNumber = [] 
    for(let i=1; i<=24100; i++){
        random = parseInt(Math.random() * (4222391 - 4158859)) + 4158859 
        if (arryNumber.indexOf(random) < 0) { //去掉重复的随机id
            arryNumber.push(random)
        }
    }
    filter = {
        _snid: {
            $in: arryNumber
        }
    }
    await new_delete(filter)
    console.log('删除完毕，删了' + arryNumber.length + '条数据')
}

deleteData()
